import { Component, OnInit, Injector, ViewChild, EventEmitter, Output } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent, FileUpload } from 'primeng/primeng';
import {
    StockZT_RoomServiceProxy,
    Stock_RoomListDto,
    ListDtoExtensionsItem,
    GetStock_RoomInput,
    UserDataPermissionServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { CreateOrEditRoomModalComponent } from './create-or-edit-room-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDaterangepickerConfig } from 'ngx-bootstrap';
import { RoomTreeComponent } from './room-tree.component';
import { IBasicRoomInfo } from './basic-room-info';

@Component({
    templateUrl: './room.component.html',
    animations: [appModuleAnimation()],
    selector: 'room'
})
export class RoomComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('ExcelFileUpload') excelFileUpload: FileUpload;
    // @ViewChild('excelModal') excelModal: ExcelModalComponent;
    @ViewChild('createOrEditRoomModal')
    createOrEditRoomModal: CreateOrEditRoomModalComponent;
    @ViewChild('ouRoomTree') ouRoomTree: RoomTreeComponent;
    @Output() save = new EventEmitter<any>();

    basicRoomInfo: IBasicRoomInfo = null;
    roomshow = 1;
    locationshow = 0;
    anyncing = false;
    filterText = '';
    userDataPermissions;
    selecttept;
    roomcode;
    selectedValues = new Array<Stock_RoomListDto>();
    minMode: BsDatepickerViewMode = 'month';

    bsConfig: Partial<BsDaterangepickerConfig>;
    extensions: Array<ListDtoExtensionsItem>;
    constructor(
        injector: Injector,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this.selecttept = result.items;
            // this.itemContextMenuItems = this.getContextMenuItems();
            this.getTableData();
        });
    }

    getTableData(event?: LazyLoadEvent) {
        this.save.emit();
        // if (!this.selectedDepts) {
        //     return;
        // }
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this.selectedValues = new Array<Stock_RoomListDto>();
        this._stockZT_RoomServiceProxy
            .getRoomListExtension(
                undefined,
                this.selecttept,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.stock_RoomList.totalCount;
                this.primengTableHelper.records = result.stock_RoomList.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getRooms(event?: LazyLoadEvent) {
        // if (!this.selectedDepts) {
        //     return;
        // }

        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._stockZT_RoomServiceProxy
            .getRoomListExtension(
                undefined,
                this.selecttept,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.stock_RoomList.totalCount;
                this.primengTableHelper.records = result.stock_RoomList.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    batchDelete() {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this.primengTableHelper.showLoadingIndicator();
                let input = new GetStock_RoomInput();
                input.filter = this.filterText;
                this._stockZT_RoomServiceProxy.barchDeleteRoom(input).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.primengTableHelper.hideLoadingIndicator();
                    this.getTableData();
                });
            }
        });
    }
    selectedDelete() {
        this.message.confirm(this.l('AreYouSure'), isConfirmed => {
            if (isConfirmed) {
                this.selectedValues.forEach(element => {
                    this._stockZT_RoomServiceProxy.deleteRoom(element.id).subscribe(() => {
                        this.notify.success(this.l('SuccessfullyDeleted'));
                        this.getTableData();
                    });
                });
            }
        });
    }

    delete(id: string) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            if (isConfirmed) {
                this._stockZT_RoomServiceProxy.deleteRoom(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getRooms();
                    this.save.emit();
                });
            }
        });
    }
}
